package com.yc.china.login;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
@Component
public class ZuulLogin extends ZuulFilter {
    // 过滤器类型 pre 表示在 请求之前进行拦截
    @Override
    public String filterType() {
        return "pre";
    }
    // 过滤器的执行顺序。当请求在一个阶段的时候存在多个多个过滤器时，需要根据该方法的返回值依次执行
    @Override
    public int filterOrder() {
        return 0;
    }
    // 判断过滤器是否生效 默认false
    @Override
    public boolean shouldFilter() {
        return true;
    }
    //拦截器的执行方法
    @Override
    public Object run() throws ZuulException {
        RequestContext currentContext = RequestContext.getCurrentContext();
        HttpServletRequest request = currentContext.getRequest();
        String userToken = request.getParameter("userToken");
        if (StringUtils.isEmpty(userToken)) {
            currentContext.setSendZuulResponse(false);
            currentContext.setResponseStatusCode(401);
            currentContext.setResponseBody("userToken是空");
            return null;
        }
        // 否则正常执行业务逻辑.....
        return null;
    }
}
